import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:intl/intl.dart';

// ignore: must_be_immutable
class InitPage extends StatefulWidget {
  String name;
  InitPage({Key? key, required this.name}) : super(key: key);

  @override
  // ignore: no_logic_in_create_state
  _InitPageState createState() => _InitPageState();
}

class _InitPageState extends State<InitPage> {
  DateTime selectedPicker = DateTime.now(); // 获取当前时间
  TimeOfDay selectedTime = const TimeOfDay(hour: 9, minute: 30); // 获取当前日期
  _selectedPickerDate() async {
    final DateTime? date = await showDatePicker(
      context: context,
      initialDate: selectedPicker,
      firstDate: DateTime(1900),
      lastDate: DateTime(2100),
      helpText: '当前日期提示',
      cancelText: '取消',
      confirmText: '确定',
    );
    if (date == null) return;
    setState(() {
      selectedPicker = date;
    });
  }

  _selectedTimeDate() async {
    final TimeOfDay? time = await showTimePicker(
      context: context,
      initialTime: selectedTime,
      helpText: '当前时间提示',
      cancelText: '取消',
      confirmText: '确定',
    );
    if (time == null) return;
    setState(() {
      selectedTime = time;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(DateFormat.yMMMMd().format(selectedPicker)),
                InkWell(
                  child: const Icon(Icons.arrow_drop_down),
                  onTap: _selectedPickerDate,
                )
              ],
            ),
            const SizedBox(height: 30),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Text(selectedTime.format(context)),
                InkWell(
                  child: const Icon(Icons.arrow_drop_down),
                  onTap: _selectedTimeDate,
                )
              ],
            ),
          ],
        ),
      ),
    );
  }
}
